package com.sanpu.kxe.base;

import android.content.Context;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;

import com.alibaba.android.arouter.launcher.ARouter;
import com.meiqia.core.callback.OnInitCallback;
import com.meiqia.meiqiasdk.util.MQConfig;
import com.orhanobut.hawk.Hawk;
import com.orhanobut.hawk.NoEncryption;
import com.sanpu.kxe.BuildConfig;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.jump.jsbridge.JsBridgeConfig;
import com.sanpu.kxe.jump.jsbridge.ServiceModule;
import com.sanpu.kxe.jump.jsbridge.ShareAppModule;
import com.sanpu.kxe.jump.shareutil.ShareConfig;
import com.sanpu.kxe.jump.shareutil.ShareManager;
import com.sanpu.kxe.utils.AppContextUtil;
import com.sanpu.kxe.utils.DeviceUtil;
import com.sanpu.kxe.utils.TimeManager;
import com.tencent.bugly.Bugly;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.smtt.sdk.QbSdk;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;

/**
 * Description：应用入口
 * Created by 薛勇军 on 2018/6/11.
 */

public class KxeApplication extends MultiDexApplication {

    private static KxeApplication instance;

    public static IWXAPI msgApi;

    public static KxeApplication getInstance() {
        if (null == instance) {
            instance = new KxeApplication();
        }
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        AppContextUtil.initApp(this);
        if (BuildConfig.DEBUG) {
            // 打印日志
            ARouter.openLog();
            // 开启调试模式(如果在InstantRun模式下运行，必须开启调试模式！线上版本需要关闭,否则有安全风险)
            ARouter.openDebug();
            //umeng
            MobclickAgent.setDebugMode(!BuildConfig.DEBUG);
            UMConfigure.setLogEnabled(true);
        }
        ARouter.init(this); // 尽可能早，推荐在Application中初始化
        MobclickAgent.openActivityDurationTrack(false);
        MobclickAgent.setScenarioType(this, MobclickAgent.EScenarioType.E_UM_NORMAL);
        UMConfigure.init(this, UMConfigure.DEVICE_TYPE_PHONE, null);
        //x5内核初始化接口
        QbSdk.initX5Environment(getApplicationContext(), new QbSdk.PreInitCallback() {
            @Override
            public void onCoreInitFinished() {
            }

            @Override
            public void onViewInitFinished(boolean b) {
            }
        });
        Hawk.init(this).setEncryption(new NoEncryption()).build();
        initModule();
    }

    public void initModule() {
        DeviceUtil.init(this);
        new Thread(new Runnable() {
            @Override
            public void run() {
                TimeManager.getInstance().initLoginTime();
            }
        }).start();

        if (!BuildConfig.DEBUG)
            Bugly.init(this, BuildConfig.BUGLY_APP_ID, BuildConfig.DEBUG);

        ShareConfig config = ShareConfig.instance()
                .qqId(CommonConfig.QQ_APPID)
                .wxId(CommonConfig.WX_APPID)
//                .wxSecret(CommonConfig.WX_SECRET)
                .debug(true);
        ShareManager.init(config);

        JsBridgeConfig.getSetting().setProtocol("MyBridge").registerDefaultModule(
                ShareAppModule.class, ServiceModule.class
        ).debugMode(true);

        MQConfig.init(this, "0f252e9ec0c9a6fad5fd06bed24acc3b", new OnInitCallback() {
            @Override
            public void onSuccess(String clientId) {

            }

            @Override
            public void onFailure(int code, String message) {

            }
        });
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        // you must install multiDex whatever tinker is installed!
        MultiDex.install(base);

        // 安装tinker
//        Beta.installTinker();
    }
}
